/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cubelabs.nexus.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cubelabs.nexus.dao.CategoryDAO;
import cubelabs.nexus.dao.CounsellingAcceptorDAO;
import cubelabs.nexus.dao.CounsellingEventDAO;
import cubelabs.nexus.dao.GuideInfoDAO;
import cubelabs.nexus.dao.HelpMeDTO;
import cubelabs.nexus.dao.ScheduledCounsellingEventDAO;
import cubelabs.nexus.dao.UserDAO;
import cubelabs.nexus.entity.Category;
import cubelabs.nexus.entity.CounsellingAcceptor;
import cubelabs.nexus.entity.CounsellingEvent;
import cubelabs.nexus.entity.GuideInfo;
import cubelabs.nexus.entity.ScheduledCounsellingEvent;
import cubelabs.nexus.entity.User;

/**
 *
 * @author chaitanya
 */
@Service
public class GuideService {
	
	
	@Autowired
	private GuideInfoDAO guideInfoDAO;
	
	
	@Autowired
	private CategoryDAO categoryDAO;
	
	
	@Autowired
	private CounsellingEventDAO counsellingEventDAO;
	
	@Autowired
	private CounsellingAcceptorDAO counsellingAcceptorDAO;
	
	
	@Autowired
	private ScheduledCounsellingEventDAO scheduledCounsellingEventDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Transactional
	public void save(GuideInfo guideInfo){
		guideInfoDAO.makePersistent(guideInfo);
	}
	
	@Transactional
	public void deleteEvent(Long eventId){
		CounsellingEvent ce = new CounsellingEvent();
		ce.setId(eventId);
		counsellingEventDAO.makeTransient(ce);
	}
	
	@Transactional
	public List<CounsellingEvent> getAllEvents(User user){
		
		 return counsellingEventDAO.findAllEventsByUserId(user);
		
		
	}
	
			
	@Transactional
	public void save(User user,HelpMeDTO hmd){
		CounsellingEvent ce =new CounsellingEvent();
		ce.setUser(user);
		ce.setStart(getDate(hmd.getStartTime()));
		ce.setEnd(getDate(hmd.getStartTime()));
		ce.setDiscription(hmd.getDescription());
		ce.setActive(true);
		ce.setNewTimeProposed(false);
		
		
		
		counsellingEventDAO.makePersistent(ce);
		
		CounsellingAcceptor ca= new CounsellingAcceptor();
		ca.setAccepted(false);
		ca.setCounsellingEvent(ce);
		ca.setUser(userDAO.findById(hmd.getHelpFrom(), false));
		counsellingAcceptorDAO.makePersistent(ca);
	}

	SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm");
	private Date getDate(String str){
		try {
			return sdf.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
		
	@Transactional
	public List<Category> getCategories() {
		return categoryDAO.findAll();
	}

	@Transactional
	public List<GuideInfo> findAllCapbilities(long userId) {
		return guideInfoDAO.fetchGuideInfo(userId);
	}

	@Transactional
	public List<CounsellingAcceptor> findPendingRequests(User user) {
		return counsellingAcceptorDAO.findPendingRequests(user);
	}

	@Transactional
	public boolean acceptHelpRequest(long caid, User user) {
		CounsellingAcceptor ca =counsellingAcceptorDAO.findById(caid, false);
		if(ca.getUser().getId()!=user.getId()){
			return false;
		}
		ca.setAccepted(true);
		//FixMe have to use a comment table for councelling and courses
		ScheduledCounsellingEvent userSc = new ScheduledCounsellingEvent();
		ScheduledCounsellingEvent mentorSc = new ScheduledCounsellingEvent();
		
		userSc.setCounsellingEvent(ca.getCounsellingEvent());
		mentorSc.setCounsellingEvent(ca.getCounsellingEvent());
		
		userSc.setUser(ca.getCounsellingEvent().getUser());
		mentorSc.setUser(user);
		
		userSc.setMentor(false);
		mentorSc.setMentor(true);
		
		scheduledCounsellingEventDAO.makePersistent(userSc);
		scheduledCounsellingEventDAO.makePersistent(mentorSc);
		
		return true;
	}
	
	@Transactional
	public boolean rejectHelpRequest(long caid, User user) {
		CounsellingAcceptor ca =counsellingAcceptorDAO.findById(caid, false);
		if(ca.getUser().getId()!=user.getId()){
			return false;
		}
		
		//FIXME naming convintion accepted = true means its either accepted or rejected 
		ca.setAccepted(true);
		ca.getCounsellingEvent().setDeclined(true);
		return true;
	}
}
